package com.qidian.common.beans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicInteger;

import com.qidian.common.TableMeta;

public class RechargeCardBean implements ISyncBean
{
    String rechargeCode ;
    int point = -1;
//    volatile int isUsed = 0;
    AtomicInteger isUsed = new AtomicInteger(0) ;
    Long userId = -1L;
    Long birthTime = System.currentTimeMillis();
    Long validTime = 0L;
    Long usedTime = 0L;
    int status = 0  ;
    
//    public static final String clearSql = "delete from " + TableMeta.table_recharge_card +
//    										" where "+ TableMeta.column_is_used + " > 0" ;
    boolean needSync = false ;
    
	public static RechargeCardBean toRechargeCardBean(ResultSet rs) throws SQLException 
	{
		RechargeCardBean accountBean = new RechargeCardBean() ;
		accountBean.setUserId(rs.getLong(TableMeta.column_user_id)) ;
		accountBean.setRechargeCode(rs.getString(TableMeta.column_recharge_code)) ;
		accountBean.setPoint(rs.getInt(TableMeta.column_point)) ;
		accountBean.isUsed.set(rs.getInt(TableMeta.column_is_used)) ;
		accountBean.usedTime = rs.getLong(TableMeta.column_used_time) ;
		accountBean.birthTime = rs.getLong(TableMeta.column_birth_time) ;
		accountBean.validTime = rs.getLong(TableMeta.column_valid_time) ;
		accountBean.setStatus(rs.getInt(TableMeta.column_status)) ;
		return accountBean;
	}
	
    public static boolean isValidRC(String RC)
    {
    	return RC != null && RC.matches("[0-9a-fA-F|]{32,32}") ;
    }
	
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public int getPoint() {
		return point;
	}
	public void setPoint(int point) {
		this.point = point;
		this.needSync = true ;
	}
	
	public boolean isTimeout(){
		
		return System.currentTimeMillis() > validTime ;
	}

    @Override
    public PreparedStatement getPreparedStatement(Connection conn)
            throws SQLException
    {
        String sql = " update " + TableMeta.table_recharge_card + " set " +
                        TableMeta.column_is_used + " = ? ," +
                        TableMeta.column_used_time + " = ?, "+
                        TableMeta.column_status + " = ? " +
                        " where " + TableMeta.column_recharge_code + " = ? ";
       
        return conn.prepareStatement(sql);
    }
    
    @Override
    public boolean needSynchronized()
    {
        return needSync;
    }
    
    @Override
    public boolean setParameters(PreparedStatement ps)
    {
        try
        {
        	ps.setInt(1, isUsed.get()) ;
        	ps.setLong(2, usedTime) ;
        	ps.setInt(3, status) ;
        	ps.setString(4,rechargeCode) ;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            try 
            {
				ps.clearParameters() ;
			}
            catch (SQLException e1) 
            {
			}
            return false ;
        }
        needSync = false ;
        return true;
    }
    
    public String toString()
    {
    	StringBuffer sb = new StringBuffer(128) ;
    	sb.append("RechargeCode:").append(this.rechargeCode) ;
    	sb.append(",Points:").append(this.point) ;
    	return sb.toString() ;
    }

	public boolean isUsed() {
		return isUsed.get() > 0;
	}

	public void setUsed(int used) {
		this.isUsed.set(used);
		this.needSync = true ;
	}

	public String getRechargeCode() {
		return rechargeCode;
	}

	public void setRechargeCode(String rechargetCode) {
		this.rechargeCode = rechargetCode;
	}

	public Long getBirthTime() {
		return birthTime;
	}

	public void setBirthTime(Long birthTime) {
		this.birthTime = birthTime;
	}

	public Long getUsedTime() {
		return usedTime;
	}

	public void setUsedTime(Long usedTime) {
		this.usedTime = usedTime;
	}

	public Long getValidTime() {
		return validTime;
	}

	public void setValidTime(Long validTime) {
		this.validTime = validTime;
	}
	public void setDeleted()
	{
	}
	public boolean isDeleted()
	{
		return false;
	}
//	public boolean isMainAccount() {
//		return isMainAccount;
//	}
//
//	public void setMainAccount(boolean isMainAccount) {
//		this.isMainAccount = isMainAccount;
//	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
		this.needSync = true ;
	}
    
}
